﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using MergeInfo.Modal.Entity;
using MergeInfo.Modal.Interface.IDAO;

namespace MergeInfo.Modal.DAO
{
    public class WebsiteDao : BaseDao, IWebsiteDao
    {
        private const string SelectSql = @"SELECT [Id]
      ,[Name]
      ,[Url]
      ,[FilePatch]
  FROM [MergeInfo].[dbo].[vWebsite]";

        public IList<WebsiteEntity> Select(Guid id)
        {
            const string sql = SelectSql + " where [Id]=@Id";
            SqlCommand = new SqlCommand { Connection = SqlConnection, CommandText = sql };
            SqlCommand.Parameters.Add(new SqlParameter("@Id", id));
            SqlCommand.Connection.Open();
            SqlDataReader sqlDataReader = SqlCommand.ExecuteReader();
            IList<WebsiteEntity> websiteEntities = AssemblyList(sqlDataReader);
            SqlCommand.Connection.Close();
            return websiteEntities;
        }

        private static IList<WebsiteEntity> AssemblyList(SqlDataReader sqlDataReader)
        {
            IList<WebsiteEntity> websiteEntities = new List<WebsiteEntity>();
            while (sqlDataReader.Read())
            {
                var websiteEntity = new WebsiteEntity
                {
                    Id = new Guid(sqlDataReader["Id"].ToString()),
                    Name = sqlDataReader["Name"].ToString(),
                    Url = sqlDataReader["Url"].ToString(),
                    FilePatch = sqlDataReader["FilePatch"].ToString(),
                };
                websiteEntities.Add(websiteEntity);
                sqlDataReader.NextResult();
            }
            return websiteEntities;
        }
    }
}
